import { Component, OnInit } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';

import { IItem, Item } from 'app/shared/model/item.model';
import { ItemService } from './item.service';
import { IItemType } from 'app/shared/model/item-type.model';
import { ItemTypeService } from 'app/entities/item-type/item-type.service';

@Component({
  selector: 'jhi-item-update',
  templateUrl: './item-update.component.html',
})
export class ItemUpdateComponent implements OnInit {
  isSaving = false;
  itemtypes: IItemType[] = [];

  editForm = this.fb.group({
    id: [],
    itemName: [],
    itemCode: [],
    soruce: [],
    shortProfile: [],
    profile: [],
    itemTypes: [],
  });

  constructor(
    protected itemService: ItemService,
    protected itemTypeService: ItemTypeService,
    protected activatedRoute: ActivatedRoute,
    private fb: FormBuilder
  ) {}

  ngOnInit(): void {
    this.activatedRoute.data.subscribe(({ item }) => {
      this.updateForm(item);

      this.itemTypeService.query().subscribe((res: HttpResponse<IItemType[]>) => (this.itemtypes = res.body || []));
    });
  }

  updateForm(item: IItem): void {
    this.editForm.patchValue({
      id: item.id,
      itemName: item.itemName,
      itemCode: item.itemCode,
      soruce: item.soruce,
      shortProfile: item.shortProfile,
      profile: item.profile,
      itemTypes: item.itemTypes,
    });
  }

  previousState(): void {
    window.history.back();
  }

  save(): void {
    this.isSaving = true;
    const item = this.createFromForm();
    if (item.id !== undefined) {
      this.subscribeToSaveResponse(this.itemService.update(item));
    } else {
      this.subscribeToSaveResponse(this.itemService.create(item));
    }
  }

  private createFromForm(): IItem {
    return {
      ...new Item(),
      id: this.editForm.get(['id'])!.value,
      itemName: this.editForm.get(['itemName'])!.value,
      itemCode: this.editForm.get(['itemCode'])!.value,
      soruce: this.editForm.get(['soruce'])!.value,
      shortProfile: this.editForm.get(['shortProfile'])!.value,
      profile: this.editForm.get(['profile'])!.value,
      itemTypes: this.editForm.get(['itemTypes'])!.value,
    };
  }

  protected subscribeToSaveResponse(result: Observable<HttpResponse<IItem>>): void {
    result.subscribe(
      () => this.onSaveSuccess(),
      () => this.onSaveError()
    );
  }

  protected onSaveSuccess(): void {
    this.isSaving = false;
    this.previousState();
  }

  protected onSaveError(): void {
    this.isSaving = false;
  }

  trackById(index: number, item: IItemType): any {
    return item.id;
  }

  getSelected(selectedVals: IItemType[], option: IItemType): IItemType {
    if (selectedVals) {
      for (let i = 0; i < selectedVals.length; i++) {
        if (option.id === selectedVals[i].id) {
          return selectedVals[i];
        }
      }
    }
    return option;
  }
}
